// See LICENSE for license details.
#ifndef _GPIO_H
#define _GPIO_H

#include "common_func.h"
#include <stdio.h> 

#ifdef __cplusplus
 extern "C" {
#endif

/**
  * @brief GPIO
  */
typedef struct {  /*!< GPIO Structure */
    volatile uint32_t PADDIR;
    volatile uint32_t PADIN;
    volatile uint32_t PADOUT;
    volatile uint32_t INTEN;
    volatile uint32_t INTTYPE0;
    volatile uint32_t INTTYPE1;
    volatile uint32_t INTSTATUS;
    volatile uint32_t IOFCFG;
} GPIO_TypeDef;

typedef enum gpio_int_type {
    GPIO_INT_RISE = 0,
    GPIO_INT_FALL = 1,
    GPIO_INT_HIGH = 2,
    GPIO_INT_LOW = 3
} GPIO_INT_TYPE;

int32_t gpio_iof_config(GPIO_TypeDef *gpio, uint32_t mask);
int32_t gpio_enable_output(GPIO_TypeDef *gpio, uint32_t mask);
int32_t gpio_enable_input(GPIO_TypeDef *gpio, uint32_t mask);
int32_t gpio_write(GPIO_TypeDef *gpio, uint32_t mask, uint32_t value);
int32_t gpio_toggle(GPIO_TypeDef *gpio, uint32_t mask);
int32_t gpio_read(GPIO_TypeDef *gpio, uint32_t mask);
int32_t gpio_enable_interrupt(GPIO_TypeDef *gpio, uint32_t mask, GPIO_INT_TYPE type);
int32_t gpio_disable_interrupt(GPIO_TypeDef *gpio, uint32_t mask);
int32_t gpio_clear_interrupt(GPIO_TypeDef *gpio);

#define GPIOA                   ((GPIO_TypeDef *) 0xbf030000)

#define IOF_SPI_MASK              0x0000003fUL
#define IOF_SPI_SCK_MASK          0x00000001UL
#define IOF_SPI_SS0_MASK          0x00000002UL
#define IOF_SPI_MOSI_MASK         0x00000004UL
#define IOF_SPI_MISO_MASK         0x00000008UL
#define IOF_SPI_DQ0_MASK          0x00000004UL
#define IOF_SPI_DQ1_MASK          0x00000008UL
#define IOF_SPI_DQ2_MASK          0x00000010UL
#define IOF_SPI_DQ3_MASK          0x00000020UL

#define IOF_I2C_MASK              0x000000c0UL
#define IOF_I2C_SCL_MASK          0x00000040UL
#define IOF_I2C_SDA_MASK          0x00000080UL


#ifdef __cplusplus
}
#endif
#endif /* _GPIO_H */
